package mil.af.amc.adpetracker.web.actions.struts2;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import mil.af.amc.adpetracker.exceptions.DuplicateUsernameException;
import mil.af.amc.adpetracker.model.Account;
import mil.af.amc.adpetracker.service.AccountService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Struts 2 action to handle login and related actions:
 * - Login
 * - Account Registration
 * - Viewing the main account view page
 * 
 * @author Jason Ferguson
 */
@ParentPackage("json-default")
@Namespace("/login")
public class LoginAction extends ActionSupport implements ModelDriven<Account> {

    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    // ModelDriven implementation

    private Account model;

    @VisitorFieldValidator(message="", appendPrefix = false)
    public Account getModel() {
        return model;
    }

    /**
     * Login to the application
     *
     * @return
     * @throws Exception
     */
    @Action(value = "login", results = {
            @Result(name="success", location = "/WEB-INF/struts2/viewAccountProfile.jsp"),
            @Result(name="input", location = "/login.jsp")
    })
    @SkipValidation
    public String login() throws Exception {

        // get the username

        throw new UnsupportedOperationException();
    }

    /**
     * Register for an account to use the application
     * 
     * @return
     * @throws Exception
     */
    @Action(value = "register", results = {
            @Result(name="success", location = "/login.jsp"),
            @Result(name="input", location = "/WEB-INF/struts2/register.jsp")
    })
    public String register() throws Exception {

        // create the account from the model, checking for DuplicateUsernameException
        Account createdAccount;
        try {
            createdAccount = accountService.createAccount(model);
        } catch (DuplicateUsernameException ex) {
            // if DUE is thrown, set an error message and return ERROR
            addActionError("Account already exists.");
            return INPUT;
        }
        
        // if DUE is not thrown, set a info message and return SUCCESS
        addActionMessage("Account created. Please check your email for login details.");
        return SUCCESS;



    }

    @Action(value="viewAccountProfile", results = {
            @Result(name="success", location = "/WEB-INF/struts2/viewAccountProfile.jsp")
    })
    public String viewAccountProfile() throws Exception {

        return SUCCESS;
    }
}
